package com.itheima.web.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@WebFilter("/*")
public class LoginFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        //强制转换
        HttpServletRequest request=(HttpServletRequest)req;
        //获取资源请求路径
        String requestURI = request.getRequestURI();
        //判断是否包含登录相关的资源路径
        if(requestURI.contains("/login.jsp")||requestURI.contains("/loginServlet")||requestURI.contains("/css/")||requestURI.contains("/js/")||requestURI.contains("/fonts/")||requestURI.contains("checkCode")){
            //包含就放行
            chain.doFilter(req,resp);
        }else {
            Object user = request.getSession().getAttribute("user");
            if(user!=null){
                chain.doFilter(req,resp);
            }else {
                request.setAttribute("msg","你尚未登录");
                request.getRequestDispatcher("login.jsp").forward(request,resp  );
            }
        }
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
